from selenium.webdriver.common.by import By

from xueqiu_app_po.base.xueqiu_app import XueQiuApp



class ShowResultPage(XueQiuApp):
    _ELEMENT1=By.XPATH,"//*[@resource-id='com.xueqiu.android:id/stock_layout']/../child::*[3]"
    _ELEMENT2=By.XPATH, "//*[@text='已添加']"
    _ELEMENT3=By.XPATH, "//*[@text='已从自选删除']"
    _ELEMENT4=By.XPATH, "//*[@text='加自选']"
    _ELEMENT5=By.XPATH, '//*[@text="取消"]'
    def click_free(self):
        # 根据子找父再找子找到加自选和已添加按钮,将按钮的当前text属性的值赋值给result
        result=self.find_and_text(*self._ELEMENT1)
        # 判断，如果按钮的文本值为加自选
        if result == '加自选':
            # 点击加自选按钮
            self.find_and_click(*self._ELEMENT1)
            # 获取到添加成功的toast标签的text值进行断言
            res = self.get_toast()

        # 按钮的文本值为已添加自选
        else:
            # 定位到该按钮并点击,用来取消自选
            self.find_and_click(*self._ELEMENT2)
            # 获取到取消自选的toast标签的text值进行断言
            res = self.find_and_text(*self._ELEMENT3)
            print(res)
            # 定位到该按钮并点击,添加自选
            self.find_and_click(*self._ELEMENT4)

        # 点击取消按钮
        self.find_and_click(*self._ELEMENT5)
        from xueqiu_app_po.page.main_page import MainPage
        return MainPage(self.driver)
